package cn.springcloud.book;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * Created by wudl on 2018/12/1.
 */
@RestController
@Slf4j
public class ConsumerController {
    @Autowired
    private HelloService helloService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ExecutorService executorService;

    @GetMapping("/helloByFeign")
    public String helloByFeign(String name) {
        log.info("client sent.Feign 方式，参数：{}", name);

        String result = helloService.sayHello(name);

        log.info("client receive.Feign方式,结果:{}", result);
        return result;
    }

    @GetMapping("/helloByRestTemplate")
    public String helloByRestTemplate(String name) {
        log.info("client sent. RestTemplate方式, 参数: {}", name);
        String url = "http://localhost:8082/sayHello?name=" + name;
        String result = restTemplate.getForObject(url, String.class);
        log.info("client received. RestTemplate方式, 结果: {}", result);
        return result;
    }

    @GetMapping("/helloByNewThread")
    public String hello(String name) throws ExecutionException, InterruptedException {
//        Future future = executorService.submit(() ->{
        log.info("client send.子线程，参数：{}", name);
        Future future = executorService.submit(() -> {
            log.info("client sent.进入子线程，参数：{}", name);
            String result = helloService.sayHello(name);
            return result;
        });
        String result = (String) future.get();
//        });
        log.info("client received.返回主线程，参数：{}", result);
        return result;

    }

}
